
from java.util import Locale
from java.lang import String
from java.lang  import System

from java.awt import Color
from java.awt  import Font

from jhplot import HPlotJa
from jhplot import P1D
from jhplot.jadraw import JaAxes
from jhplot.jadraw import JaText
from jhplot.jadraw import JaKey

from jscinttools.utilities.jheputilities import HPlotJaUtil


Locale.setDefault(Locale.ENGLISH)

showFigures = True
writeFigures = not showFigures

""" Define colors and fonts """
figType = [".png", ".eps"]
darkGreen = Color(0, 170, 0)
purple = Color(128, 0, 128)
darkGold = Color(255, 193, 37)
colors = [Color.blue, Color.red, darkGreen, Color.magenta, Color.black, purple, darkGold]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 20)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
ticFontZoom = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 15)

""" Define the Path and files names """
rangeVal = [2.5, 6]
rangeZoom = [2.95, 2.98]


indPhotFile = "/home/marc-antoine/Documents/BGO/QFin/ScintEventBGO/indPhot"

outPath = "/home/marc-antoine/Documents/Test"
outName = "SingleEvent300K"


""" 
###############################################################
# Create the P1D
###############################################################
"""
indivPhot = P1D("Photons", indPhotFile, style="p", color=colors[1], symbol=6, symbolSize=8, errAll=False)


"""
###############################################################
# Plotting Part
###############################################################
"""
indivPhotKey = JaKey(indivPhot.symbol, indivPhot.title, 0.165, 0.775, "NDC", color=indivPhot.color, keyColor=indivPhot.color, penWidth=2, font=legendFont)



""" Create the Plotter """
mainPlotter = HPlotJa("Single Event", 600, 600, 2, 1, True)
mainPlotter.setGTitle("Scintillation Event in BGO with ^{22}Na at 300 K", titleFont)
mainPlotter.setAntiAlias(False)
if showFigures:
	mainPlotter.visible(True)
else:
	mainPlotter.visible(False)
mainPlotter.cd(1,1)
mainPlotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
mainPlotter.setRange(rangeVal[0], rangeVal[1], -0.85, 0.1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, False)
mainPlotter.setTicFont(ticFont)
mainPlotter.setShowStatBox(False)
mainPlotter.showKey(False)
mainPlotter.setNameX("Time (&micro;s)")
mainPlotter.setNameY("Amplitude (V)")

mainPlotter.cd(2,1)
mainPlotter.setPad(2, 1, 0.465, 0.455, 0.35, 0.35)
mainPlotter.setRange(rangeZoom[0], rangeZoom[1], -0.5, 0.1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, False)
mainPlotter.setTicFont(ticFont)
mainPlotter.setShowStatBox(False)
mainPlotter.showKey(False)
mainPlotter.setNameX("")
mainPlotter.setNameY("")

""" Plots all the JHep objects """
mainPlotter.cd(1,1)
mainPlotter.draw(indivPhot)
mainPlotter.draw(indivPhotKey)

mainPlotter.cd(2,1)
mainPlotter.draw(indivPhot)


""" Modifies the JaAxes and the axes labels """
labX_11 = mainPlotter.getLabelX()[0][0]
labX_11.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, labX_11, mainPlotter)
labY_11 = mainPlotter.getLabelY()[0][0]
labY_11.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, labY_11, mainPlotter, -0.01)

mainPlotter.cd(1,1)
theAxes = mainPlotter.getPad()
theAxes.setLabelSpace(0, 0.0)
theAxes.setLabelSpace(1, 0.0)

mainPlotter.cd(2,1)
theAxes = mainPlotter.getPad()
theAxes.setLabelSpace(0, 0.0)
theAxes.setLabelSpace(1, 0.0)

mainPlotter.update()

""" Saves or not the plot """
if writeFigures:
	for i in range(len(figType)):
		outFileName = outName + figType[i]
		mainPlotter.export(outPath + File.separator + outFileName)
		print("Writting File : " + outPath + File.separator + outFileName)
		
mainPlotter.update()
print("That's all folks !")

